package com.mccken.annotation.hystrix;

import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import com.mccken.annotation.DoHystrix;
import com.mccken.annotation.hystrix.value.IValueService;
import com.mccken.annotation.hystrix.value.impl.HystrixValueImpl;

/**
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2022-12-24 15:15
 **/
@Component
@Aspect
public class DoHystrixPoint {
	
	@Pointcut("@annotation(com.mccken.annotation.DoHystrix)")
	public void aopPoint(){
		
	}
	
	@Around("aopPoint() && @annotation(doGovern)")
	public Object doRouter(ProceedingJoinPoint jp, DoHystrix doGovern) throws Throwable {
		IValueService valueService = new HystrixValueImpl();
		return valueService.access(jp, getMethod(jp), doGovern, jp.getArgs());
	}
	
	private Method getMethod(JoinPoint jp) throws NoSuchMethodException {
		Signature signature = jp.getSignature();
		MethodSignature methodSignature = (MethodSignature) signature;
		return jp.getTarget().getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
	}
}
